
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 票api
 */
 
@Api(tags = "Ticket",description = "票")
@RestController
public class TicketController {
    
    @Autowired
    private TicketDao dao;
   
    /**
     * 获取票集合
     * @return api结果
     */
    @ApiOperation(value = "获取票集合")
    @RequestMapping(value =  "/api/Ticket/",method = {RequestMethod.GET})
    public ApiResult<List<Ticket>> Get(){
         ApiResult<List<Ticket>> result=new ApiResult<List<Ticket>>();
        try 
        {
            List<Ticket> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取票信息
     * @param id 票ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取票信息")
    @RequestMapping(value =  "/api/Ticket/{id}",method = {RequestMethod.GET})
    public ApiResult<Ticket> Get(@PathVariable Long id){
        ApiResult<Ticket> result=new ApiResult<Ticket>();
        try
        {
            Ticket model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改票信息
     * @param id 票ID
     * @param model 票信息
     * @return api结果
     */
    @ApiOperation(value = "修改票信息")
    @RequestMapping(value =  "/api/Ticket/{id}",method = {RequestMethod.PUT})
    public ApiResult<Ticket> Edit(@PathVariable Long id,@RequestBody Ticket model){
        ApiResult<Ticket> result=new ApiResult<Ticket>();
        try
        {
            model.setTicketId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加票信息
     * @param model 票信息
     * @return api结果
     */
    @ApiOperation(value = "添加票信息")
    @RequestMapping(value =  "/api/Ticket/",method = {RequestMethod.POST})
    public ApiResult<Ticket> Add(@RequestBody Ticket model){
         ApiResult<Ticket> result=new ApiResult<Ticket>();
        try
        {
            if  (model.getProjectId()==""){
                result.setHasError(true);
                result.setMessage("项目ID不能为空");
            }else{ 
                model.setCreateDate(new Date());
                dao.save(model);
                result.setData(model);
           }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除票信息
     * @param id 票ID
     * @return api结果
     */
    @ApiOperation(value = "删除票信息")
    @RequestMapping(value =  "/api/Ticket/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询票种集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询票种集合")
    @RequestMapping(value =  "/api/Ticket/Search",method = {RequestMethod.POST})
    public ApiResult<List<Ticket>> Search(@RequestBody PageTicket model){
         ApiResult<List<Ticket>> result=new ApiResult<List<Ticket>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"sign_date") ;            
            Page<Ticket> page = dao.findByAll(model.getName(),model.getCode(),model.getProjectId(),model.getTicketTypeId(),model.getIdNumber(),model.getIsPassage(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

}

